package ar.edu.unq.wannaplay.panels.project;

import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;

import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.pages.project.EditProjectPage;
import ar.edu.unq.wannaplay.services.ProjectService;
import ar.edu.unq.wannaplay.utils.AjaxConfirmSwapListButton;
import ar.edu.unq.wannaplay.utils.ProjectEditer;

public class EditProjectEmployeesPanel extends Panel {
    private static final long serialVersionUID = 1L;

    private List<EmployeeDTO> allEmployees;

    private List<EmployeeDTO> projectEmployees;

    // private List<EmployeeDTO> current;

    private ProjectEditer modelObject;

    private ProjectService service;

    public EditProjectEmployeesPanel(final String id, final ProjectEditer modelObject, final ProjectService service) {
        super(id);
        this.modelObject = modelObject;
        this.service = service;
        this.initialize();
    }

    @SuppressWarnings("rawtypes")
    private void initialize() {
        projectEmployees = new LinkedList<EmployeeDTO>(modelObject.getEmployeesDTO());

        allEmployees = service.findAllEmployeesDTO();
        allEmployees.removeAll(projectEmployees);

        ListMultipleChoice<EmployeeDTO> from = this.createListMultipleChoice("from", projectEmployees,
                "projectEmployees");
        ListMultipleChoice<EmployeeDTO> to = this.createListMultipleChoice("to", allEmployees, "allEmployees");

        AjaxButton fromTo = new AjaxConfirmSwapListButton("fromTo", projectEmployees, from, to);
        AjaxButton toFrom = new AjaxConfirmSwapListButton("toFrom", allEmployees, to, from);
        Button acceptButton = this.createSubmitButton("accept");

        Form employeeChoiceForm = new Form("employeeChoiceForm");
        employeeChoiceForm.add(from);
        employeeChoiceForm.add(to);
        employeeChoiceForm.add(fromTo);
        employeeChoiceForm.add(toFrom);
        employeeChoiceForm.add(acceptButton);

        this.add(employeeChoiceForm);

    }

    @SuppressWarnings("synthetic-access")
    private Button createSubmitButton(final String id) {
        Button acceptButton = new Button(id) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                modelObject.setEmployeesDTO(projectEmployees);
                service.updateProjectEmployees(modelObject.getId(), projectEmployees);
                this.setResponsePage(new EditProjectPage(modelObject, service));
            }
        };
        return acceptButton;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private ListMultipleChoice<EmployeeDTO> createListMultipleChoice(final String id, final List<EmployeeDTO> bind,
            final String prop) {
        PropertyModel propertyModel = new PropertyModel(this, prop);
        ListMultipleChoice list = new ListMultipleChoice<EmployeeDTO>(id, propertyModel, this.createLDM(bind));
        list.setOutputMarkupId(true);
        return list;
    }

    @SuppressWarnings("rawtypes")
    private LoadableDetachableModel createLDM(final List<EmployeeDTO> list) {

        return new LoadableDetachableModel() {
            private static final long serialVersionUID = 1L;

            @Override
            protected Object load() {
                return list;
            }
        };

    }

    public List<EmployeeDTO> getAllEmployees() {
        return allEmployees;
    }

    public void setAllEmployees(final List<EmployeeDTO> allEmployees) {
        this.allEmployees = allEmployees;
    }

    public List<EmployeeDTO> getProjectEmployees() {
        return projectEmployees;
    }

    public void setProjectEmployees(final List<EmployeeDTO> projectEmployees) {
        this.projectEmployees = projectEmployees;
    }

    public ProjectEditer getModelObject() {
        return modelObject;
    }

    public void setModelObject(final ProjectEditer modelObject) {
        this.modelObject = modelObject;
    }

    public ProjectService getService() {
        return service;
    }

    public void setService(final ProjectService service) {
        this.service = service;
    }

}
